package com.example.memolaga.cards;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.StringReader;
import java.util.ArrayList;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import com.example.memolaga.Constants;
import com.example.memolaga.Functions;
import com.example.memolaga.password.FunctionsPass;

import android.util.Log;

public class CardsManager{

    //private byte[] sKey;

	private static CardsManager instance = null;

	private ArrayList<CardsMaster> cardSaved;  //L'elenco delle carte salvate con i giusti dati criptati

    private CardsManager(){this.cardSaved = new ArrayList<CardsMaster>();}

	public static CardsManager getInstance(){ if (instance==null) instance = new CardsManager(); return instance;};

    public int addCardFromFile(String path)
    {
        if (!(new File(path).exists())) return 0;
        int size = this.cardSaved.size();
        loadData(Functions.getStringOfFile(path));
        return this.cardSaved.size()-size;
    }

	public int getNCards()
	{
        if (this.cardSaved==null) return 0;
        return this.cardSaved.size();
	}

    public ArrayList<CardsMaster> getCardSaved()
    {
        return this.cardSaved;
    }
	

	/**
	 * Funzione che dato il nome della carta, ritorna la classe con tutti i valori
	 * @param group
	 * @return
	 */
	public  CardsMaster getCard(String group)
	{
        if (this.cardSaved!=null)
		for(int i=0; i<this.cardSaved.size();i++)
			if (group.equals(this.cardSaved.get(i).getName()))
                return this.cardSaved.get(i);
		return null;
	}

    public CardsMaster getCardIndex(int i)
    {
        if (i<this.cardSaved.size())
            return this.cardSaved.get(i);
        else
            return null;
    }



	public void cardAdd(String name, String number,
			String pin, String cvv, String phone, String circuits, String expiry, String balance,
            String holder, String billAddress, String bank, String website, String note, String sKey)
    {
		this.cAdd(name,number,pin,cvv,phone,circuits,expiry,balance,holder,billAddress,bank,
                website,note,sKey);
		//return this.cardSaved;
	}

    public void cardAdd(ArrayList<CardsMaster> toAdd, String sKey)
    {
        for (int i=0;i<toAdd.size();i++)
        {
            this.cAdd(toAdd.get(i).getName(),toAdd.get(i).getNumber(),toAdd.get(i).getPin(),toAdd.get(i).getCVV(),
                    toAdd.get(i).getPhoneToBlock(), toAdd.get(i).getCircuits(), toAdd.get(i).getExpiry(),
                    toAdd.get(i).getBalance(), toAdd.get(i).getHolder(), toAdd.get(i).getBillAddress(),
                    toAdd.get(i).getBank(), toAdd.get(i).getWebSite(), toAdd.get(i).getNote(), sKey);
        }
        //return this.cardSaved;
    }

    public boolean modCard(String name, String number,
                           String pin, String cvv, String phone, String circuits, String expiry, String balance,
                           String holder, String billAddress, String bank, String website, String note, String sKey)
    {
        for (int i=0;i<this.cardSaved.size(); i++)
            if (name.equals(this.cardSaved.get(i).getName()))
            {
                CardsMaster cNew = new CardsMaster();
                cNew.setName(name);
                cNew.setNumber(FunctionsPass.encryptString(sKey,number));
                cNew.setPin(FunctionsPass.encryptString(sKey,pin));
                cNew.setCVV(FunctionsPass.encryptString(sKey,cvv));
                cNew.setPhoneToBlock(phone);
                cNew.setCircuits(circuits);
                cNew.setExpiry(expiry);
                cNew.setBalance(balance);
                cNew.setHolder(holder);
                cNew.setBillAddress(billAddress);
                cNew.setBank(bank);
                cNew.setWebSite(website);
                cNew.setNote(note);
                this.cardSaved.add(i,cNew);
                this.cardSaved.remove(i+1);
                return true;
            }
        return false;
    }

    public boolean delCard(String name)
    {
        for(int i=0; i<this.cardSaved.size();i++)
            if (name.equals(this.cardSaved.get(i).getName()))
            {
                this.cardSaved.remove(i);
                return true;
            }
        return false;
    }

    private void cAdd(String name, String number,
                         String pin, String cvv, String phone, String circuits, String expiry, String balance,
                         String holder, String billAddress, String bank, String website, String note, String sKey)
    {
        CardsMaster cNew = new CardsMaster();
        cNew.setName(name);
        cNew.setNumber(FunctionsPass.encryptString(sKey,number));
        cNew.setPin(FunctionsPass.encryptString(sKey,pin));
        cNew.setCVV(FunctionsPass.encryptString(sKey,cvv));
        cNew.setPhoneToBlock(phone);
        cNew.setCircuits(circuits);
        cNew.setExpiry(expiry);
        cNew.setBalance(balance);
        cNew.setHolder(holder);
        cNew.setBillAddress(billAddress);
        cNew.setBank(bank);
        cNew.setWebSite(website);
        cNew.setNote(note);
        this.cardSaved.add(cNew);
    }

    public String getStringToWrite(ArrayList<CardsMaster> toWrite)
    {
        String strToWrite = Constants.getHeadXmlFile() + "\n" + Constants.getHeadOpenXML()+Constants.getCreditCardsRoot()+Constants.getTailXML() + "\n";
        if (toWrite!=null)
            for (int i=0; i<toWrite.size(); i++)
            {
                CardsMaster c = toWrite.get(i);
                strToWrite = strToWrite + Constants.getHeadOpenXML()+Constants.getCardsElement()+Constants.getTailXML() + "\n";
                strToWrite = strToWrite + Constants.getHeadOpenXML()+Constants.getCardsName()+Constants.getTailXML() + c.getName() + Constants.getHeadCloseXML()+Constants.getCardsName()+Constants.getTailXML() + "\n";
                strToWrite = strToWrite + Constants.getHeadOpenXML()+Constants.getCardsNumber()+Constants.getTailXML() + c.getNumber() + Constants.getHeadCloseXML()+Constants.getCardsNumber()+Constants.getTailXML() + "\n";
                strToWrite = strToWrite + Constants.getHeadOpenXML()+Constants.getCardsPIN()+Constants.getTailXML() + c.getPin() + Constants.getHeadCloseXML()+Constants.getCardsPIN()+Constants.getTailXML() + "\n";
                strToWrite = strToWrite + Constants.getHeadOpenXML()+Constants.getCardsCVV()+Constants.getTailXML() + c.getCVV() + Constants.getHeadCloseXML()+Constants.getCardsCVV()+Constants.getTailXML() + "\n";
                strToWrite = strToWrite + Constants.getHeadOpenXML()+Constants.getCardsPhone()+Constants.getTailXML() + c.getPhoneToBlock() + Constants.getHeadCloseXML()+Constants.getCardsPhone()+Constants.getTailXML()+ "\n";
                strToWrite = strToWrite + Constants.getHeadOpenXML()+Constants.getCardsCircuits()+Constants.getTailXML() + c.getCircuits() + Constants.getHeadCloseXML()+Constants.getCardsCircuits()+Constants.getTailXML() + "\n";
                strToWrite = strToWrite + Constants.getHeadOpenXML()+Constants.getCardsExpiry()+Constants.getTailXML() + c.getExpiry() + Constants.getHeadCloseXML()+Constants.getCardsExpiry()+Constants.getTailXML() + "\n";
                strToWrite = strToWrite + Constants.getHeadOpenXML()+Constants.getCardsBalance()+Constants.getTailXML() + c.getBalance() + Constants.getHeadCloseXML()+Constants.getCardsBalance()+Constants.getTailXML() + "\n";
                strToWrite = strToWrite + Constants.getHeadOpenXML()+Constants.getCardHolder()+Constants.getTailXML() + c.getHolder() + Constants.getHeadCloseXML()+Constants.getCardHolder()+Constants.getTailXML()+"\n";
                strToWrite = strToWrite + Constants.getHeadOpenXML()+Constants.getCardBillAddress()+Constants.getTailXML() + c.getBillAddress() + Constants.getHeadCloseXML()+Constants.getCardBillAddress()+Constants.getTailXML()+"\n";
                strToWrite = strToWrite + Constants.getHeadOpenXML()+Constants.getCardsBank()+Constants.getTailXML() + c.getBank() + Constants.getHeadCloseXML()+Constants.getCardsBank()+Constants.getTailXML() + "\n";
                strToWrite = strToWrite + Constants.getHeadOpenXML()+Constants.getCardsWebSite()+Constants.getTailXML() + c.getWebSite() + Constants.getHeadCloseXML()+Constants.getCardsWebSite()+Constants.getTailXML() + "\n";
                strToWrite = strToWrite + Constants.getHeadOpenXML()+Constants.getCardsNote()+Constants.getTailXML() + c.getNote() + Constants.getHeadCloseXML()+Constants.getCardsNote()+Constants.getTailXML() + "\n";
                strToWrite = strToWrite + Constants.getHeadCloseXML()+Constants.getCardsElement()+Constants.getTailXML() + "\n";
            }
        strToWrite = strToWrite + Constants.getHeadCloseXML()+Constants.getCreditCardsRoot()+Constants.getTailXML();
        return strToWrite;
    }

    private void loadData(String xml)
    {
        try {
            Log.w("Cards()", "StartParseXML");
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();

            CardsXMLHandler myXMLHandler = new CardsXMLHandler();
            xr.setContentHandler(myXMLHandler);

            xr.parse(new InputSource(new StringReader(xml)));
            //Log.w("AndroidParseXMLActivity", "Parse3");

            ArrayList<CardsMaster> itemsList = myXMLHandler.getItemsList();
            if (itemsList.size()>0)
            {
                this.cardSaved = new ArrayList<CardsMaster>();

                for(int i=0;i<itemsList.size();i++){
                    //	CardsMaster item = itemsList.get(i);
                    this.cardSaved.add(itemsList.get(i));
                }
            }
            Log.w("AndroidParseXMLActivity", "Done");
        }
        catch (FileNotFoundException foe)
        {
            Log.i("createFile",foe.getMessage());
        }
        catch (Exception e) {
            Log.w("AndroidParseXMLActivity",e );
        }
    }
	

}
